package com.carl.main;

import com.carl.mapper.MaoYanCommentMapper;
import com.carl.mapper.MaoYanMapper;
import com.carl.model.MaoYan;
import com.carl.model.MaoYanComment;
import com.carl.utils.MybatisHelper;
import org.apache.ibatis.session.SqlSession;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.Date;


public class Day05_MaoYanCrawler {

     /**
          regex-wholeData :  div.content div.board-item-content
          regex-maoyanData :
                    name :         p.name
                    star :         p.star
                    releasetime :  p.releasetime
                    score :        p.score
                    url :          p.name a[href]
                    commentList :
                         summary : div.main-content div.mod-content span.dra
                         comment :


      */
     // 猫眼电影 : http://maoyan.com/board/7
     public static void main(String[] args)throws Exception{

          crawler_maoyan();
     }

     protected static void crawler_maoyan()throws Exception{
          SqlSession sqlSession = MybatisHelper.getSqlSessionLocal() ;
          MaoYanMapper maoYanMapper = sqlSession.getMapper(MaoYanMapper.class);
          MaoYanCommentMapper maoYanCommentMapper = sqlSession.getMapper(MaoYanCommentMapper.class);

          String crawlerUrl = "http://maoyan.com/board/7";
          String maoyanUrl = "http://maoyan.com";

          Document document = Jsoup.connect(crawlerUrl).get();
          Elements elements = document.select("div.content div.board-item-content");
          if (elements != null) {
               for (Element element : elements) {
                    MaoYan maoYan = new MaoYan();
                    String dataName = element.select("p.name").text().trim();
                    String dataStar = element.select("p.star").text().trim();
                    String dataScore = element.select("p.score").text().trim();
                    String dataReleaseTime = element.select("p.releasetime").text().trim();

                    maoYan.setFilmsName(dataName);
                    maoYan.setFilmsStar(dataStar);
                    maoYan.setFilmsScore(dataScore);
                    maoYan.setFilmsCrawlerTime(new Date());
                    maoYan.setFilmsReleaseTime(dataReleaseTime);

                    String dataUrl = maoyanUrl + element.select("p.name a").attr("href");
                    Document commentDocument = Jsoup.connect(dataUrl).get();
                    String dataSummary = commentDocument.select("div.main-content div.mod-content span.dra").text().trim();
                    maoYan.setFilmsSummary(dataSummary);

                    maoYanMapper.insert(maoYan);
                    sqlSession.commit();

                    Elements commentElements = commentDocument.select("div.mod-content div.main");
                    for (Element commentElement : commentElements) {
                         MaoYanComment maoYanComment = new MaoYanComment();
                         String commentUser = commentElement.select("div.user span.name").text();
                         String commentUpTime = commentElement.select("div.time").attr("title");
                         String commentScore = commentElement.select("ul.score-star").attr("data-score");
                         String commentContent = commentElement.select("div.comment-content").text();
                         String commentApprove = commentElement.select("div.approve span").text();
                         commentContent = commentContent.replaceAll("[\\x{10000}-\\x{10FFFF}]", "");
                         commentUser = commentUser.replaceAll("[\\x{10000}-\\x{10FFFF}]", "");

                         maoYanComment.setFilmsId(maoYan.getFilmsId());
                         maoYanComment.setCommentUser(commentUser);
                         maoYanComment.setCommentScore(commentScore);
                         maoYanComment.setCommentUpTime(commentUpTime);
                         maoYanComment.setCommentContent(commentContent);
                         maoYanComment.setCommentCrawlerTime(new Date());
                         maoYanComment.setCommentClickNum(new Integer(commentApprove));

                         maoYanCommentMapper.insert(maoYanComment);
                         sqlSession.commit();
                    }

               }
          }

          sqlSession.close();
     }

}
